﻿package Base.Sound 
{
	//import Log.LogManager;
	
	import Common.GameDefine;
	
	import flash.events.*;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.system.Capabilities;
	
	/**
	 * ...
	 * @author fox
	 */
	public class SoundManager 
	{
		private var valid:Boolean = false;
		private var soundMap:Object = new Object;
		private var transform:SoundTransform;
		private var transformAll:SoundTransform;
		private var playingMap:Object = new Object();
		private var playingMusic:SoundChannel = null;
		private var playingPath:String = "";
		
		
		public function isValid():Boolean
		{
			return valid;
		}
		
		
		static public var instance:SoundManager = null;
		static public function Instance():SoundManager
		{
			if ( instance == null )
			{
				instance = new SoundManager();
			}
			
			return instance;
		}
		
		
		public function SoundManager()
		{
			
		}
		
		
		public function init():void
		{
			if ( Capabilities.hasAudio && Capabilities.hasMP3 )
			{
				// swf && mp3 sound
				
				valid = true;
			}
			
			transformAll = new SoundTransform();
			transformAll.volume = 1.0;
			
			transform = new SoundTransform();
			transform.volume = 1.0;
			
			SoundMixer.soundTransform = transformAll;
		}
		
		
		public function setVolume( num:Number ):void
		{
			transformAll.volume = num;
			SoundMixer.soundTransform = transformAll;
		}
		
		
		public function Load( path:String ):void
		{
			if ( !valid )
			{
				return;
			}
			
			if ( soundMap[ path ] != null )
			{
				return;
			}
			
			var url:URLRequest = new URLRequest( GameDefine.url + path );
			var sound:Sound = new Sound();
			sound.addEventListener( IOErrorEvent.IO_ERROR , onError );
			sound.load( url );
			
			soundMap[ path ] = sound;
		}
		
		
		public function onError( event:Event ):void
		{
			trace( event );
		}
		
		
		public function PlaySount( path:String ):void
		{
			if ( !valid )
			{
				return;
			}
			
			if ( soundMap[ path ] )
			{
				var channel:SoundChannel = soundMap[ path ].play( 0 , 1 );
				
				if ( channel )
				{
					channel.soundTransform = transform;	
				}
				
				playingMusic = channel;
			}
		}
		
		
		public function PlayMusic( path:String ):void
		{
			if ( !valid )
			{
				return;
			}
			
			if ( path == playingPath )
			{
				return;
			}
			
			if ( playingMusic )
			{
				playingMusic.stop();
			}
			
			if ( soundMap[ path ] )
			{
				playingPath = path;
				
				var channel:SoundChannel = soundMap[ path ].play( 0 , int.MAX_VALUE );
				transform.volume = 1.0;
				if ( channel )
				{
					channel.soundTransform = transform;	
				}
				
				playingMusic = channel;
			}
		}
		
		
		public function StopMusic():void	
		{
			if ( playingMusic )
			{
				playingPath = "";
				playingMusic.stop();
				playingMusic = null;
			}
		}
		
		
		///////////////////////////////////////////////////////
		
		// on complete
		private function onComplete( event:Event ):void
		{
			//LogManager.logOut( event.toString() );
		}
		
	}
	
}